<template>
  <div class="login">
  <div class="box">
    <h2>选课管理系统</h2>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="用户名" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password" type="password"></el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
    </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  name: 'Login',
  data() {
      return {
        ruleForm: {
          userName: '',
          password:''
        },
        rules: {
          userName: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' }
          ],
        }
      };
    },
     methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post('api/login',this.ruleForm).then(res=>{
                if(res.data.code==200){
                    console.log(res.data.data.token)
                    this.$store.commit('SET_TOKEN',res.data.data.token)
                    this.$store.commit('SET_USERINFO',res.data.data.userInfo)
                    this.$message({
                        message: res.data.message,
                        type: 'success'
                    });
                    this.$router.push('/')
                }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }

}
</script>
<style scoped>
.login{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: antiquewhite;
}
.box{
    width:400px;
    height: 200px;
}
h2{
    margin-bottom: 20px;
    font-size: 32px;
}
</style>
